package com.lemon.components.ant.pom;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.lemon.components.ant.beans.ProjectInfo;

public class PomManager {
	
	Pattern pattern = Pattern.compile("<dependency>"
			+ "\\s*<groupId>(\\S*)</groupId>"
			+ "\\s*<artifactId>(\\S*)</artifactId>"
			+ "\\s*<version>(\\S*)</version>"
			+ "\\s*</dependency>",Pattern.CASE_INSENSITIVE);
	
	public void test(ProjectInfo project) {
		File pomFile = new File(project.getProjectPath()+File.separatorChar+project.getArtifactId()+"/pom.xml");
		StringBuilder pomString = new StringBuilder();
		try (BufferedReader reader = new BufferedReader(new FileReader(pomFile))){
			String tmp = null;
			while ((tmp = reader.readLine())!=null) {
				pomString.append(tmp);
				pomString.append("\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		Matcher matcher = pattern.matcher(pomString.toString());
		while(matcher.find()) {
			System.out.println("-->"+matcher.group(0));
		}
	}

	public static void main(String[] args) {
		PomManager pomManager = new PomManager();
		ProjectInfo project = new ProjectInfo();
		project.setProjectPath("D:/workspace");
		project.setArtifactId("carshop");
		
		pomManager.test(project);
	}

}
